package cn.yedong.basic.controller;

import cn.yedong.basic.dto.PhoneCodeDto;
import cn.yedong.basic.exception.BusinessException;
import cn.yedong.basic.service.IVerifyService;
import cn.yedong.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 获取校验
 */
@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {
    @Autowired
    private IVerifyService verifyService;

    /**
     * 获取图形验证码
     * @param key
     * @return
     */
    @GetMapping("/image/{key}")///verifyCode/image/5d1b952c-1ba0-4e43-8f42-5165001caaa7
    public AjaxResult getImageCode(@PathVariable("key") String key){
        try {
            //返回一个通过base64编码的字符串
            String base64Str = verifyService.getImageCode(key);
            return AjaxResult.me().setObj(base64Str);
        } catch (BusinessException e) {
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"亲，网络繁忙，请稍后再试！");
        }
    }

    /**
     * 获取手机验证码
     * @param phoneCodeDto
     * @return
     */
    @PostMapping("/phone")
    public AjaxResult getPhoneCode(@RequestBody PhoneCodeDto phoneCodeDto){
        try {
            verifyService.sendPhoneCode(phoneCodeDto);
            return AjaxResult.me();
        } catch (BusinessException e) {
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"亲，网络忙，请稍后再试！");
        }
    }
    /**
     * 获取微信绑定手机验证码
     * @param phoneCodeDto
     * @return
     */
    @PostMapping("/wechatBinder")
    public AjaxResult wechatBinderCode(@RequestBody PhoneCodeDto phoneCodeDto){
        try {
            verifyService.wechatBinderCode(phoneCodeDto);
            return AjaxResult.me();
        } catch (BusinessException e) {
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"亲，网络忙，请稍后再试！");
        }
    }
}
